home *** CD-ROM | disk | FTP | other *** search
/ BCI NET / BCI NET Dec 94.iso / archives / applications / wp / macro.lha / Macro / TeX-Scripts / TeXedit.rexx < prev    next >
OS/2 REXX Batch file  |  1994-03-24  |  14KB  |  417 lines

  1. /*RX
  2.  * AREXX        Name:TeXedit.rexx       version:1.41    date:27-Jul-91
  3.  *
  4.  This ARexx script is called from virtex (or initex) in case of an
  5. error or if the 'e' command is used, and it's given the current file
  6. and line number as argument. It may be called from the TeX-server too.
  7. We will successively call CygnusEd (CED) or Micro(GNU)Emacs (MG) and GoldED to
  8. load the file and the logfile. Other editors welcome!
  9.  *
  10. INPUTS:
  11.  1: filename to edit (please no spaces in filename)
  12.  2: line where error occured
  13.  *
  14. BUGS:
  15.  GETCLIP("TEXFILE") gets precedence over any other information for
  16. retrieving the logfile, so if you alternately start virtex directly
  17. from CLI while the server is active, the wrong files may be loaded.
  18.  *
  19.  This file tries to cope with the following version (PasTeX 1.2a)
  20. > This is a PD-Version of Pas-TeX (made Jan 30 1991 [br]/[hes])
  21. > This is TeX, C Version 3.1
  22. (The logfile is sometimes put at the wrong place.)
  23.  >> I have only tried with Version 3.14 (Rene Laederach)
  24.  *
  25.  Does not handle names relative to the local root correctly (like ":foo/bar")
  26.  *
  27.  See each editor relative bugs below.
  28.  *
  29. FILES:
  30.  Rexx:namestruc
  31.  LIBS:rexxsupport.library
  32. AUTHORS:
  33.  *      J\"org H\"ohle, since March 91
  34.  *      Georg Hessmann, previous version
  35.  *      Ren\'e\ Laederach for GoldED part
  36.  *
  37. TODO
  38.  * I believe it is necessery to store the number of the GoldED which started
  39.  * the compile.
  40.  *
  41.  *
  42.  
  43. Done by:
  44.  
  45. R.Laederach
  46. Kappelisackerstr. 46
  47. 3063 Ittigen
  48. Switzerland
  49. Phone:+41/(0)31/912 19 08
  50.  
  51. This stuff ist PostcardWare, so send me a postcard or something useful (read
  52. the doc!).
  53.  
  54. */
  55.  
  56. IF ~SHOW('L','rexxsupport.library') THEN
  57.         IF ~ADDLIB('rexxsupport.library',0,-30,0) THEN DO
  58.                 say "Can't open 'rexxsupport.library'!"
  59.                 EXIT 20
  60.                 END
  61.  
  62. /* The TeX server may want to know that we have been called */
  63. CALL SETCLIP("TEXTIME",TIME('s'))
  64.  
  65. PARSE ARG filename number .
  66.  
  67. /*say address()*/
  68. /* if the TeX 'e' command was used to call the editor, don't ask if
  69.         files should be loaded. */
  70. IF address() = 'TeX-Rexx-Port' & "EDIT" ~= UPPER(mygetenv("TEXREXX"))
  71. THEN askload = 0
  72. ELSE askload = 1
  73.  
  74. /**
  75. OPTIONS RESULTS
  76. namestruc filename
  77. IF 0~= RC THEN DO
  78.         say "TeXEdit.rexx: Function "namestruc" not found!"
  79.         EXIT 10
  80.         END
  81. ELSE PARSE VALUE RESULT WITH ivol idirs ibase .
  82. **/
  83. PARSE VALUE namestruc(filename) WITH ivol idirs ibase .
  84.  
  85. /* The idea behind the following statements is to get an absolute path
  86. for <filename>. The result will be stored in <errnfile>. For example, "CD
  87. TeX:bar" "virtex foo" should give <filename>=foo, and
  88. <errnfile>=TeX:bar/foo. This is necessary because the editor doesn't
  89. know where file foo is located, but will be able to load file
  90. TeX:bar/foo. */
  91.  
  92. mainname = GETCLIP("TEXFILE")
  93. if "" == mainname THEN DO
  94.    /* TeX has not been started through some Start_TeX.#? script. */
  95.    /* We have a hard time finding the right directories */
  96.  
  97.    texdir = pragma('d')
  98.  
  99.    /* Amiga OS dirnames should end with either : or /
  100.         thus you need just append the filename */
  101.    IF RIGHT(texdir,1)~=':' & RIGHT(texdir,1)~='/' THEN texdir = texdir||'/'
  102.  
  103.    IF 0 = ivol THEN DO
  104.         errnfile = texdir||SUBSTR(filename, 1+ivol)
  105.         /* The logfile is in the current dir */
  106.         logfile = texdir||SUBSTR(filename, 1+ivol+idirs, ibase)||".log"
  107.         END
  108.    ELSE DO
  109.         errnfile=filename
  110.         IF 0 = idirs THEN
  111. /* This is a bug in virtex from PasTeX12a (made Jan 30 1991 [br]/[hes])
  112.         The logfile is *not* in virtex's directory, but rather in the
  113. source's directory if a device name, but no subsequent directory was
  114. given. I.e, virtex (CD in RAM) TeX:sample would put the errorfile in
  115. TeX: and not in RAM: as it should be. */
  116.            logfile = LEFT(filename, ivol+idirs+ibase)||".log"
  117.         ELSE logfile = texdir||SUBSTR(filename, 1+ivol+idirs, ibase)||".log"
  118.         END
  119.    DROP texdir
  120.    END
  121.  
  122. ELSE DO /* TeX was started through Start_TeX.#? scripts */
  123.         /* "TEXFILE" tells us CD and the main file */
  124. /**
  125.    namestruc mainname
  126.    PARSE VALUE RESULT WITH mvol mdirs mbase .
  127. **/
  128.    PARSE VALUE namestruc(mainname) WITH mvol mdirs mbase .
  129.  
  130.    IF 0 ~= ivol THEN errnfile = filename
  131.    ELSE errnfile = LEFT(mainname, mvol+mdirs)||SUBSTR(filename, 1+ivol)
  132.    logfile = LEFT(mainname, mvol+mdirs+mbase)||".log"
  133.    DROP mvol mdirs mbase
  134.    END
  135.  
  136.  
  137. /* 0 = ibase would mean that the call was incorrect, for example when
  138.    TEXREXXEDIT says "TeXedit.rexx" and not "TeXedit.rexx %s %d" */
  139. IF 0 = ibase | ~EXISTS(errnfile) THEN DO
  140.         say "TeXedit.rexx: Can't find erroneous file."
  141.         EXIT 10
  142.         END
  143. ELSE IF ~EXISTS(logfile) THEN DO
  144.         say "TeXedit.rexx: Can't find log file."
  145.         logfile = ""
  146.         /* but we continue */
  147.         END
  148.  
  149. DROP ivol idirs ibase
  150.  
  151. /********************************************************/
  152. /*      Here starts each editor specific parts          */
  153.  
  154. /************************/
  155. /*      Cygnus Ed       */
  156. IF SHOW('P', 'rexx_ced') THEN DO
  157.  
  158. ADDRESS "rexx_ced"
  159. OPTIONS RESULTS
  160.  
  161. cedtofront
  162.  
  163. /* testen ob die Files nicht schon geladen sind. (hes) */
  164.  
  165. /* the following should not be commented out, as this macro is called
  166. asynchronously, and it would be very bad for the user to have it's
  167. input mixed with the newly loaded files. Popping up a requester
  168. ensures that the user does not type in something and waits for the
  169. files to be loaded. This is the method I use for synchronization (JCH) */
  170.  
  171. IF askload THEN DO
  172.         okay2 'TeX found an error in 'errnfile'. Load files ?'
  173.         IF 1 ~= RESULT THEN EXIT 0
  174.         END
  175.  
  176. /* TODO: We should really delete old logfiles automatically, without letting
  177.         CED open a requester to ask if an old modified file may be
  178.         overwritten or not */
  179.  
  180. IF "" ~= logfile THEN DO
  181.         'jump to file "'logfile'"'
  182.         IF 0 ~= RESULT THEN Quit /* always assume the logfile currently
  183.                 loaded is old, because TeX generated a new one. */
  184.         Open new
  185.         /*expand view*/
  186.         open '"'logfile'"'
  187.         editable file   /* now it's non-editable */
  188.         beg of file
  189.         /* if we don't have the line number: search the number in the logfile (hes) */
  190.         /* bringt das wirklich etwas ? Der erste Fehler steht
  191.                 sowieso gegen Anfang der Logdatei */
  192.         IF 0 ~= number THEN 'Search for...' "l."||number
  193.         ELSE DO
  194.           'Search for...' "l." /* ^M bringt nichts, und dann Steuercodes hier...*/
  195.           'Right'
  196.           'Status 55'           /* take the current line from CED */
  197.           PARSE VAR RESULT "l."number .
  198.         END
  199.         /* changed from Search "! " (hes) */
  200.         END
  201. ELSE    okay1 "Couldn''t find logfile associated with TeX error!"
  202.  
  203. /* the following line should really read "jump to file errnfile" and
  204. not filename, because I have done extra work to get the right
  205. directory. But this information is only in errnfile, which contains an
  206. absolute pathname, and not in filename, which is the parameter
  207. supplied by virtex or initex, usually a relative pathname. CED's
  208. current directory is not necessarily virtex'es one. It also seems that
  209. CED is not smart enough as to make the difference between
  210. foo:tgmoae/myfile and bar:tgmoab/myfile. If I say jump to file
  211. foo:tgmoae/myfile it may as well jump to bar:tgmoab/myfile, whichever
  212. window comes first. */
  213.  
  214. 'jump to file "'errnfile'"'
  215. IF 0 = RESULT THEN DO
  216.         open new
  217.         open '"'errnfile'"'
  218.         END
  219.  
  220. /*expand view*/
  221.  
  222. /* TODO: another editor may have modified the disk file, or the user
  223. the currently loaded file, while virtex compiled an old version. How
  224. can I get rid of that stupid filerequester in that case? I said "open
  225. errnfile", so why does CED pop up a filerequester (and may put the
  226. user in the wrong directory too) ? */
  227.  
  228. IF 0~= number THEN jumpto number
  229. beg of line
  230.  
  231. EXIT 0
  232. END     /* End of CED part */
  233.  
  234.  
  235. /****************************************************************/
  236. /*      Micro(Gnu)Emacs (MG), to be found on AmigaLibDisk352    */
  237. IF SHOW('P','mg') THEN DO
  238.  
  239. ADDRESS 'mg'
  240. OPTIONS RESULTS
  241.  
  242. /* The following waits until MG is deiconified */
  243. 'amiga-window-to-top'   /* should activate window ? */
  244.  
  245. /* we need to prevent the user from continuing to type and thus
  246. modifying the newly created buffers. I choose the rexx-lock/unlock
  247. method to synchronise with the user because MG3b4 has some cursor
  248. position/display bugs: the cursor may erroneously appear in some
  249. buffer and overwrite it (on the screen only), and not appear in the
  250. bottom line where it belongs. */
  251.  
  252. IF askload THEN DO
  253.         'rexx-lock'     /* this is dangerous if this script aborts */
  254.         ADDRESS VALUE RESULT
  255.  
  256.         'rexx-request "TeX found an error. Load files ? "'
  257.         retc=RC
  258.         rets=RESULT
  259.  
  260.         'rexx-unlock'
  261.         ADDRESS VALUE RESULT
  262.  
  263.         IF 1 < retc /* abort */ | LEFT(UPPER(rets),1)='N' THEN DO
  264.                 'rexx-display ""'
  265.                 EXIT 0
  266.                 END
  267.         DROP retc rets
  268.         END
  269.  
  270. 'find-file "'errnfile'"'
  271. /* MG doesn't seem to set the RC value on a find-file */
  272. 'rexx-buffer' buf
  273. /* buf.1 is the buffer name, buf.3 the number of lines */
  274. IF number < buf.3 THEN DO
  275.         'delete-other-windows'
  276.         IF "" ~= logfile THEN DO
  277.                 'split-window-vertically'
  278.                 /* 'other-window' */
  279.  
  280. /* now get rid of old logfiles. Here I make sure that I get rid of
  281. every suspicious logfile, because multiple pathes may lead to the same
  282. file, as for example DH0:TeX/sample.log & TeX:sample.log */
  283.  
  284.                 'rexx-buffer-list' buffers
  285.                 PARSE VALUE namestruc(logfile) WITH ivol idirs ibase
  286.                 logname=UPPER(SUBSTR(logfile,1+ivol+idirs))
  287.                 DO i=1 TO buffers.0
  288.                    IF 0 < INDEX(UPPER(buffers.i.file),logname) THEN DO
  289.                         'switch-to-buffer "'buffers.i.name'"'
  290.                         'not-modified'
  291.                         'kill-buffer "'buffers.i.name'"'
  292.                         END
  293.                    END
  294.  
  295.                 'find-file "'logfile'"'
  296.                 /* mg doesn't seem to set the RC value */
  297.                 'beginning-of-buffer'
  298.                 IF 0 ~= number THEN 're-search-forward "^l."number'
  299.                 ELSE DO
  300.                    /* try to use normal search ? */
  301.                    're-search-forward "^l."'    /* ^ means beg. of line */
  302.                    IF 0 = RC THEN DO    /* search successfull */
  303.                         'rexx-line'     /* get current line contents */
  304.                         PARSE VAR RESULT "l."number .
  305.                         /*number = WORD(RESULT, 2) ?*/
  306.                         END
  307.                    END
  308.                 'other-window'
  309.                 'rexx-display "Now what''s that error?"'
  310.                 END
  311.         ELSE DO
  312.                 'rexx-display "Couldn''t find logfile associated with TeX error!"'
  313.                 /* EXIT 0 * 5? */
  314.                 END
  315.         IF 0 ~= number THEN 'goto-line 'number
  316.         END
  317. ELSE DO
  318.         'not-modified'
  319.         'kill-buffer "'buf.name'"'      /* kill this empty buffer */
  320.         'rexx-display "Couldn''t find erroneous file !"'
  321.         EXIT 5
  322.         END
  323. EXIT 0
  324. END     /* End of MG part*/
  325.  
  326.  
  327. /***********************************************/
  328. /*                    GoldED                   */
  329. /* Done by R.Laederach                         */
  330.  
  331. IF SHOW('P', 'GOLDED.1') THEN DO /* Here I should get the right ARexx port on */
  332.                                  /* the spot, but how can I know which GoldED */
  333.                                  /* was used to edit the text when coming from*/
  334.                                  /* virtex? */
  335.  
  336.  
  337. /* $VER: 0.9, ©1993 Dietmar Eilert. Empty GoldED macro */
  338.  
  339. OPTIONS RESULTS                             /* enable return codes     */
  340.  
  341. if (LEFT(ADDRESS(), 6) ~= "GOLDED") then    /* not started by GoldEd ? */
  342.     address 'GOLDED.1'
  343.  
  344. 'LOCK CURRENT'                              /* lock GUI, gain access   */
  345. OPTIONS FAILAT 6                            /* ignore warnings         */
  346. SIGNAL ON SYNTAX                            /* ensure clean exit       */
  347.  
  348. IF askload THEN DO
  349.     'REQUEST BUTTON="_Yep!|_Nope!" BODY="TeX found an error in the TeX source file. Load Files?"'
  350.     if (RESULT = 'TRUE') THEN EXIT 0
  351.     END
  352.  
  353. IF "" ~= logfile THEN DO
  354.     'REQUEST HIDE = TRUE'
  355.     'WINDOW HEIGHT=80 QUIET FORCE USE="'||logfile'"'
  356.     'OPEN AGAIN' /* 'WINDOW USE FORCE' does NOT reload file (needed
  357.                      because log file was changed in last compile
  358.                      > Improved a bit.         */
  359.     'GOTO TOP UNFOLD=TRUE'
  360.     IF 0 ~= number THEN DO
  361.         'FIND NEXT STRING=l.'||number
  362.         'FIRST'
  363.         'QUERY BUFFER'
  364.         PARSE VAR RESULT "l." . errorstring
  365.         say errorstring
  366.         IF "" ~= errorstring THEN DO
  367.             wordnumber = WORDS(errorstring)
  368.             errorword = WORD(errorstring,wordnumber)
  369.         END
  370.     else DO
  371.         'FIND NEXT STRING="l."'
  372.         'FIRST'
  373.         'QUERY BUFFER'
  374.         PARSE VAR RESULT "l." . errorstring
  375.         say errorstring
  376.         IF "" ~= errorstring THEN DO
  377.             wordnumber = WORDS(errorstring)
  378.             errorword = WORD(errorstring,wordnumber)
  379.         END
  380.     END
  381. END
  382.     else DO
  383.         'REQUEST BODY="Could not find logfile associated with TeX error"'
  384.         UNLOCK
  385.         END
  386.  
  387. 'WINDOW FORCE USE="'errnfile'"'
  388.  
  389. IF 0 ~= number THEN DO
  390.     'GOTO COLUMN=1 LINE="'number'"'
  391.     
  392.     'FIND QUIET NEXT STRING="'||errorword'"' /* Got him! */
  393.     'UNLOCK'  /* Important: Calling unlock.ged sucks! */
  394.     END
  395.  
  396. EXIT 0
  397. END          /* End of GoldED part */
  398.  
  399.  
  400. SAY "TeXedit.rexx: No supported editor active."
  401.  
  402. /* Maybe I'll build in Vim support for those purists... (I'm myself one (:-()
  403. */ 
  404.  
  405. EXIT 10
  406.  
  407. mygetenv: procedure     /* when will ARexx supply GetEnv/SetEnv ? */
  408.    PARSE ARG name
  409.  
  410.    IF open(TEMPFILE,"ENV:"||name,'r') THEN DO
  411.         gives = readln(TEMPFILE)
  412.         CALL close TEMPFILE
  413.         END
  414.    ELSE gives = ""
  415.  
  416.    RETURN gives
  417.